from flask import Flask,jsonify,request
from config.myconfig import Myconfig
from flask_cors import CORS
from mydb import db1
from utils.getdata import getdata
import json

app = Flask(__name__)

app.config.from_object(Myconfig)
CORS(app)

@app.route('/addorder',methods=['POST'])
def addorder():
    data = getdata()
    msql = "select * from area where a_area='%s' and b_area='%s'"%(data['send_address'],data['receive_address'])
    mres = db1.find(msql)
    sql = "insert into orders(order_no,send_address,receive_address,receiver,receive_mobile,company,money,status) values('%s','%s','%s','%s','%s','%s',%.2f,0)"%(data['order_no'],data['send_address'],data['receive_address'],data['receiver'],data['receive_mobile'],data['company'],float(mres['price']))
    db1.update(sql)
    return jsonify({"code":200,'msg':'添加成功'})

@app.route('/ordershow')
def ordershow():
    sql = "select * from orders where status=0"
    res = db1.find_all(sql)
    return jsonify({"code":200,"list":res})
@app.route('/dirvershow')
def dirvershow():
    sql = "select * from driver where status=0"
    res = db1.find_all(sql)
    sql1 = "select * from car where status=0"
    res1 = db1.find_all(sql1)
    return jsonify({"code":200,"dlist":res,"clist":res1})

@app.route('/sureorder',methods=["POST"])
def sureorder():
    data = getdata()
    dsql = "select * from driver where id=%d"%int(data["driver_id"])
    dres = db1.find(dsql)
    dsql1 = "update driver set status=1 where id=%d"%int(data["driver_id"])
    db1.update(dsql1)

    csql = "select * from car where id=%d" % int(data["car_id"])
    cres = db1.find(csql)
    csql1 = "update car set status=1 where id=%d" % int(data["car_id"])
    db1.update(csql1)

    ordersql = "select * from orders where id=%d"%int(data["order_id"])
    orderres = db1.find(ordersql)
    ordersql1 = "update orders set status=1 where id=%d" % int(data["order_id"])
    db1.update(ordersql1)
    lst=[]
    params = {"car":str(cres["car_number"]),"driver":str(dres['name'])}
    lst.append(params)
    params = json.dumps(lst,ensure_ascii=False)
    sql = "update orders set params='%s' where id=%d"%(str(params),int(data["order_id"]))
    db1.update(sql)
    return jsonify({"code":200,"msg":"订单交付成功"})

# 中转订单查询
@app.route('/transfershow')
def transfershow():
    address = request.args.get("address")
    sql = "select * from orders where transfer_station='%s'"%address
    res = db1.find_all(sql)
    csql = "select * from car where location='%s' and status=0"%address
    cres = db1.find_all(csql)
    dsql = "select * from driver where location='%s' and status=0"%address
    dres = db1.find_all(dsql)

    return jsonify({"code":200,"list":res,"dlist":dres,"clist":cres,"msg":"获取成功"})


@app.route('/suretransfer',methods=["POST"])
def suretransfer():
    data = getdata()
    dsql = "select * from driver where id=%d" % int(data["driver_id"])
    dres = db1.find(dsql)
    dsql1 = "update driver set status=1 where id=%d" % int(data["driver_id"])
    db1.update(dsql1)

    csql = "select * from car where id=%d" % int(data["car_id"])
    cres = db1.find(csql)
    csql1 = "update car set status=1 where id=%d" % int(data["car_id"])
    db1.update(csql1)

    ordersql = "select * from orders where id=%d" % int(data["order_id"])
    orderres = db1.find(ordersql)
    lst = json.loads(orderres["params"])
    print(lst)
    lst.append({"car":str(cres["car_number"]),"driver":str(dres['name'])})
    # ordersql1 = "update orders set status=2 where id=%d" % int(data["order_id"])
    # db1.update(ordersql1)
    params = json.dumps(lst,ensure_ascii=False)
    sql = "update orders set params='%s' where id=%d" % (str(params), int(data["order_id"]))
    db1.update(sql)
    tsql = "insert into transfer(arrive_address,receiver,mobile,order_id) values('%s','%s','%s',%d)"%(dres["location"],dres["name"],dres["mobile"],int(data["order_id"]))
    db1.update(tsql)

    return jsonify({"code": 200, "msg": "订单交付成功"})

@app.route('/arriveshow')
def arriveshow():
    address = request.args.get("address")
    sql = "select * from orders where status=2 and receive_address='%s'"%address
    res = db1.find_all(sql)
    rsql = "select * from receiver where location='%s'"%address
    rres = db1.find_all(rsql)
    return jsonify({"code":200,"list":res,"rlist":rres})

import time
@app.route('/surereceive',methods=["POST"])
def surereceive():
    data = getdata()
    stime = time.time()


if __name__ == '__main__':
    app.run()
